package gui;

import gui.abstracts.TelasPMPE;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.JButton;
import javax.swing.JPanel;

public class PanelLogin extends TelasPMPE{
	/**
	 * 
	 */
	private static final long serialVersionUID = 6327487231805052915L;
	public JTextField textLogin = null;
	public JPasswordField textSenha = null;
	private JButton btnLogar = null;
	private JLabel lblImagem = null;
	private JLabel lblLogin = null;
	private JLabel lblSenha = null;
	static private PanelLogin panelLogin = null;

	
	private PanelLogin() {
		super();
		this.setarPreferredSize();
		this.setLayout(null);
		this.startup();
	}

	private void startup() {
		add(getImagem());
		setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		add(getBotao());
		add(getPanelDados());
	}
	
	public static PanelLogin getPanelLogin(){
		if(panelLogin == null){
			panelLogin = new PanelLogin();
		}
		panelLogin.clearFields();
		return panelLogin;
	}

	public void clearFields(){
		this.getPasswordField().setText("");
		this.getLoginField().setText("");
	}

	private JTextField getLoginField() {
		if(textLogin == null){
			this.textLogin = new JTextField();
			textLogin.setBounds(55, 8, 125, 20);
		}
		return textLogin;
	}

	private JPasswordField getPasswordField() {
		if(textSenha == null){
			this.textSenha = new JPasswordField();
			textSenha.setBounds(55, 35, 125, 20);
		}
		return textSenha;
	}

	private JLabel getLblLogin(){
		if(lblLogin == null){
			lblLogin = new JLabel("login :");
			lblLogin.setBounds(10, 11, 35, 14);
		}
		return lblLogin;
	}

	private JLabel getLblSenha(){
		if(lblSenha == null){
			lblSenha = new JLabel("senha :");
			lblSenha.setBounds(3, 38, 42, 14);
		}
		return lblSenha;
	}

	
	private JButton getBotao(){
		if(btnLogar == null){
			btnLogar = new JButton("Logar");
			btnLogar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					atualizarTela();
				}
			});
			btnLogar.setBounds(63, 169, 89, 23);
		}
		return btnLogar;
	}

	private JLabel getImagem(){
		if(lblImagem == null){
			lblImagem = new JLabel("IMAGEM");
			lblImagem.setBounds(47, 8, 127, 75);
			lblImagem.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 26));
		}
		return lblImagem;

	}

	
	

	private JPanel getPanelDados() {
		JPanel panelDados = new JPanel();
		panelDados.setBounds(10, 82, 190, 66);
		
		panelDados.setLayout(null);
		panelDados.add(getLoginField());
		panelDados.add(getPasswordField());
		panelDados.add(getLblLogin());
		panelDados.add(getLblSenha());
		
		return panelDados;
	}

	protected void atualizarTela() {
		TelaInicial.getTelaInicial().atualizarTela(this,textLogin.getText(),textSenha.getPassword());	
	}

	@Override
	public
	String getTitle() {
		return "PontoPMPE";
	}

	@Override
	public void setarPreferredSize() {
		this.setPreferredSize(new Dimension(212, 232));	
	}
}